package cn.sdhuijin.vms.service;

import javax.inject.Inject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import cn.sdhuijin.vms.dao.IUserDao;
import cn.sdhuijin.vms.model.User;

@Service
public class UserService implements IUserService {
	final Logger logger = LoggerFactory.getLogger(UserService.class);
	@Inject
	private IUserDao userDao;

	@Override
	public void save(User user) {
		userDao.save(user);
	}

	@Override
	public Page<User> getList(Integer start, Integer limit) {
		return userDao.findAll(new PageRequest(start, limit));
	}

	@Override
	public void delete(Long id) {
		userDao.delete(id);
	}

	@Override
	public void login(User user, Boolean rememberMe) {
		UsernamePasswordToken token = new UsernamePasswordToken(
				user.getUsername(), user.getPassword(), rememberMe);
		SecurityUtils.getSubject().login(token);
	}

	@Override
	public void logout() {
		SecurityUtils.getSubject().logout();
	}

	@Override
	public User load(Long userId) {
		return userDao.findOne(userId);
	}

}
